package org.iabako.client.ui.form.business.paymentrequest;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.*;
import org.iabako.client.ui.layout.template.Popup;
import org.iabako.client.ui.layout.widget.emailpreview.EmailForm;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.PaymentRequestConfig;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.iservice.business.PaymentRequestServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.List;

/**
 * Created by jose on 30/01/15.
 */
public class PaymentRequestMailForm extends AbstractForm implements EmailForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, PaymentRequestMailForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final PaymentRequestServiceAsync paymentRequestServiceAsync = PaymentRequestServiceAsync.Util.getInstance();
    @UiField
    InputText subject;
    @UiField
    InputTextArea customBody;
    @UiField
    InputTextArea footer;
    @UiField
    LabelText seeCompleteMailVersion;
    @UiField
    Button save;
    @UiField
    InputText from;
    @UiField
    LabelText defaultBody;
    @UiField
    LabelText title;

    private boolean mailBeforeContent;

    public PaymentRequestMailForm() {
        simplePopup = true;
        setForm(binderForm, PageConfigEnum.PaymentRequestMailModify, null);

        Popup.resizeToMediumPopup();

        Enterprise enterprise = AuthenticateUserProvider.getUser().getEnterprise();

        from.setValue(AuthenticateUserProvider.getUser().getEnterprise().getEmail());
        subject.setValue(Messages.getLabel("payment_request_mail_subject_value", "payment_request_mail_default_body_param1"));
        defaultBody.setHtmlLabel(Messages.getLabel("payment_request_mail_before_default_body",
                "payment_request_mail_default_body_param0",
                "payment_request_mail_default_body_param1",
                "payment_request_mail_default_body_param2",
                "payment_request_mail_default_body_param3"));

        customBody.setValue(Messages.getLabel("payment_request_mail_custom_body_default_value"));
        footer.setValue(Messages.getLabel("payment_request_mail_footer", enterprise.getName()));

        setHandlers();
    }

    protected void setMailBeforeFields() {
        mailBeforeContent = true;
        title.setLabel("payment_request_mail_before_title");
        PaymentRequestConfig paymentRequestConfig = ((PaymentRequestConfigForm) parentForm).getPaymentRequestConfig();

        if (!GenericTools.isEmpty(paymentRequestConfig.getEmailBeforePaymentBody())) {
            customBody.setValue(paymentRequestConfig.getEmailBeforePaymentBody());
        }

        if (!GenericTools.isEmpty(paymentRequestConfig.getEmailBeforePaymentFooter())) {
            footer.setValue(paymentRequestConfig.getEmailBeforePaymentFooter());
        }
    }

    protected void setMailAfterFields() {
        title.setLabel("payment_request_mail_after_title");
        PaymentRequestConfig paymentRequestConfig = ((PaymentRequestConfigForm) parentForm).getPaymentRequestConfig();

        defaultBody.setHtmlLabel(Messages.getLabel("payment_request_mail_after_default_body",
                "payment_request_mail_default_body_param0",
                "payment_request_mail_default_body_param1",
                "payment_request_mail_default_body_param2",
                "payment_request_mail_default_body_param3"));

        customBody.setValue(paymentRequestConfig.getEmailAfterPaymentBody());

        if (!GenericTools.isEmpty(paymentRequestConfig.getEmailAfterPaymentFooter())) {
            footer.setValue(paymentRequestConfig.getEmailAfterPaymentFooter());
        }
    }

    public String getFrom() {
        return Messages.getLabel("payment_request_mail_from") + " " + from.getValue();
    }

    public String getSubject() {
        return Messages.getLabel("payment_request_mail_subject_label") + " " + subject.getValue();
    }

    public String getBody() {
        String body = defaultBody.getLabelText();
        body += "<br/><br/>" + customBody.getValue();
        body += "<br/><br/>" + footer.getValue();

        return body;
    }

    public String getContentNote() {
        return "payment_request_mail_body_help";
    }

    private void setHandlers() {

        final PaymentRequestMailForm parentForm = this;

        seeCompleteMailVersion.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                NavigationController.getInstance().closePopup(true);
                NavigationController.getInstance().goTo(PageEnum.EmailPreview, parentForm, null);
            }
        });

        save.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                validate();
            }
        });
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {

        PaymentRequestConfig paymentRequestConfig = ((PaymentRequestConfigForm) parentForm).getPaymentRequestConfig();

        if (mailBeforeContent) {
            paymentRequestConfig.setEmailBeforePaymentBody(customBody.getValue());
            paymentRequestConfig.setEmailBeforePaymentFooter(footer.getValue());
        } else {
            paymentRequestConfig.setEmailAfterPaymentBody(customBody.getValue());
            paymentRequestConfig.setEmailAfterPaymentFooter(footer.getValue());
        }

        paymentRequestServiceAsync.save(paymentRequestConfig, new IabakoAsyncCallback<PaymentRequestConfig>() {

            public void onSuccess(PaymentRequestConfig result) {
                popup.handleClickClose();
            }
        });
        return true;
    }
}
